//
//  Scene_ViewController.m
//  EggSteward
//
//  Created by 王道林 on 16/9/3.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "Scene_ViewController.h"
#import "BaseTableView.h"
#import "DLMacro.h"
#import "BaseButton.h"
#import "BaseLabel.h"
#import "BaseView.h"
#import "DLWNetworkingParser.h"
#import "ActionCycleModel.h"
#import "Scene_tableViewCellNearO.h"
#import "Scene_tableViewCellNearT.h"
#import "Home_NewsViewControllerWeb.h"
#import "ActionSearchViewController.h"

@interface Scene_ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) BaseButton *buttonNear;
@property (nonatomic, strong) BaseButton *buttonMy;
@property (nonatomic, strong) NSMutableArray *arrNearOne;
@property (nonatomic, strong) NSMutableArray *arrNearTwo;
@end
@implementation Scene_ViewController

NSInteger record = 0;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 创建tableView
    self.navigationItem.title = @"现场活动";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStyleDone target:self action:@selector(pushSearchVc)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    [self handleData];
    [self createSubView];
}

#pragma mark - 创建子控件
- (void)createSubView
{
    _tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self createTableHeaderView];
    [self.view addSubview:_tableView];
    // 注册nearOCell;
    [_tableView registerClass:[Scene_tableViewCellNearO class] forCellReuseIdentifier:@"reuseOne"];
    [_tableView registerClass:[Scene_tableViewCellNearT class] forCellReuseIdentifier:@"reuseTwo"];

}


#pragma mark - 获取数据
- (void)handleData
{
    _arrNearOne = [NSMutableArray array];
    _arrNearTwo = [NSMutableArray array];
    // 第二个cell数据
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *ticket = [userDefault valueForKey:@"ticket"];
    NSString *url = [NSString stringWithFormat:@"http://api.danjiguanjia.com/active/Index/getList?ticket=%@&lat=38.88971&lon=121.550833", ticket];
    [DLWNetworkingParser GETWithURL:url par:nil success:^(id responseObject) {
        for (NSDictionary *dic in responseObject[@"data"]) {
            ActionCycleModel *model = [ActionCycleModel modelWithDic:dic];
            [_arrNearTwo addObject:model];
        }
        [_tableView reloadData];
    } filed:^(NSError *error) {
        
    }];
    // 第一个cell的数据
    NSString *urlT = [NSString stringWithFormat:@"http://api.danjiguanjia.com/active/Index/getBanner?type=2&ticket=%@", ticket];
    [DLWNetworkingParser GETWithURL:urlT par:nil success:^(id responseObject) {
        
        for (NSDictionary *dic in responseObject[@"data"]) {
            ActionCycleModel *model = [ActionCycleModel modelWithDic:dic];
            [_arrNearOne addObject:model];
        }
        [_tableView reloadData];

    } filed:^(NSError *error) {
        
    }];

}


#pragma mark - tableView 的协议方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (record == 0) {
        return 2;
    } else
    {
        return 0;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (record == 0) {
        if (section == 0) {
            return _arrNearOne.count;
        } else
        {
            return _arrNearTwo.count;
        }
    } else
    {
        return 0;
    }
}
#pragma mark - 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (record == 0) {
        if (indexPath.section == 0) {
            
            Scene_tableViewCellNearO *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseOne" forIndexPath:indexPath];
            ActionCycleModel *model = _arrNearOne[indexPath.row];
            cell.model = model;
            return cell;
        } else
        {
            Scene_tableViewCellNearT *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseTwo" forIndexPath:indexPath];
            ActionCycleModel *model = _arrNearTwo[indexPath.row];
            cell.model = model;
            return cell;
        }
    } else
    {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseOne" forIndexPath:indexPath];
    cell.backgroundColor = COLORAUTO;
    return cell;
    }
}

// 返归section的头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (record == 0) {
        if (section == 0) {
            return nil;
        } else
        {
            
            BaseLabel *lable = [[BaseLabel alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 20)];
            lable.font = [UIFont systemFontOfSize:20];
            lable.text = @"附近活动";
            return lable;
        }
    }
    else
    {
        return nil;
    }
}
// cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (record == 0) {
        if (indexPath.section == 0) {
            return 200;
        } else
            return 220;
    } else
    {
        return 0;
    }
}

// 创建tableView的头视图
- (void)createTableHeaderView
{
    BaseView *view = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 40)];
    view.backgroundColor = [UIColor whiteColor];
    _tableView.tableHeaderView = view;
    _buttonNear = [BaseButton buttonWithType:UIButtonTypeSystem];
    _buttonNear.frame = CGRectMake(0, 0, VIEW_WIDTH / 2, 40);
    [_buttonNear setTitle:@"附近活动" forState:UIControlStateNormal];
    _buttonNear.titleLabel.textAlignment = 1;
    [_buttonNear setTitleColor:[UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000]forState:UIControlStateNormal];
    _buttonNear.backgroundColor = [UIColor whiteColor];
    // 点击button
    [_buttonNear addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_buttonNear];
    
    _buttonMy = [BaseButton buttonWithType:UIButtonTypeSystem];
    _buttonMy.frame = CGRectMake(VIEW_WIDTH / 2, 0, VIEW_WIDTH / 2, 40);
    [_buttonMy setTitle:@"我的活动" forState:UIControlStateNormal];
    _buttonMy.titleLabel.textAlignment = 1;
    [_buttonMy setTitleColor:[UIColor grayColor]forState:UIControlStateNormal];
    _buttonMy.backgroundColor = [UIColor colorWithWhite:0.911 alpha:1.000];
    // 点击
    [_buttonMy addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_buttonMy];
}

// button的点击方法

- (void)buttonAction:(BaseButton *)button
{
    if (button == _buttonNear) {
        // 记录变量
        record = 0;
        // 改变button颜色
        [_buttonNear setTitleColor:[UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000]forState:UIControlStateNormal];
        _buttonNear.backgroundColor = [UIColor whiteColor];
        [_buttonMy setTitleColor:[UIColor grayColor]forState:UIControlStateNormal];
        _buttonMy.backgroundColor = [UIColor colorWithWhite:0.911 alpha:1.000];
        // 刷新tableview
        [_tableView reloadData];
    } else if (button == _buttonMy)
    {
        // 记录变量
        record = 1;
        // 改变button颜色
        [_buttonMy setTitleColor:[UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000]forState:UIControlStateNormal];
        _buttonMy.backgroundColor = [UIColor whiteColor];
        [_buttonNear setTitleColor:[UIColor grayColor]forState:UIControlStateNormal];
        _buttonNear.backgroundColor = [UIColor colorWithWhite:0.911 alpha:1.000];
        // 刷新tableView
        [_tableView reloadData];
    }
}
#pragma mark - cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (record == 0) {
        if (indexPath.section == 0) {
            ActionCycleModel *model = _arrNearOne[indexPath.row];
            Home_NewsViewControllerWeb *webVC = [[Home_NewsViewControllerWeb alloc] init];
            webVC.name = @"actionNearOne";
            webVC.url = model.url;
            webVC.navigationTitle = @"活动详情";
            [self.navigationController pushViewController:webVC animated:YES];
        }
        else
        {
                ActionCycleModel *model = _arrNearTwo[indexPath.row];
                Home_NewsViewControllerWeb *webVC = [[Home_NewsViewControllerWeb alloc] init];
                webVC.name = @"actionNearTwo";
                webVC.url = model.url;
            webVC.navigationTitle = @"活动详情";
                [self.navigationController pushViewController:webVC animated:YES];
        }
    }
}

#pragma mark - 推出搜索界面
- (void)pushSearchVc
{
    ActionSearchViewController *searchVC = [[ActionSearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
